<!DOCTYPE html>
<meta charset="utf-8"/>
<title>WebSocket Test</title>
<div>
    <input type="text" id="token" name="token" value="5d72314a-7bc3-4d02-8f36-d7c8fcb25074" placeholder="token">
    <button onclick="connect()">连接</button>
    <button onclick="disConnect()">断开连接</button>
    <input type="text" id="to" name="to" value="1019110100000073" placeholder="to">
    <input type="text" id="from" name="from" value="290" placeholder="from">
    <input type="text" id="openId" name="openId" value="ouKuh51ArSMpBQtynZ4X1rJOEoz0" placeholder="openId">
    <input type="text" id="appCode" name="appCode" value="gh_7cbebd7ef237" placeholder="appCode">

    <input type="text" id="userOpenId" name="userOpenId" value="8b2d3d20-76a1-41bc-972d-7a5bc75f1999" placeholder="userOpenId">
    <input type="text" id="fsOpenId" name="fsOpenId" value="4d8de8e2-2689-46da-a658-6366afb90e7c" placeholder="fsOpenId">

    <input type="text" id="appType" name="appType" value="4" placeholder="appType 1:微信 4抖音">
    <br>
    <div>接受消息：</div>

</div>
<script language="javascript" type="text/javascript">
    <!--	改成自己电脑的内网ip-->
    //开发
     var wsUri = "ws://47.111.185.135:8088/ws";
    //测试
   // var wsUri = "ws://47.110.104.140:8088/ws";

    var output;

    function init() {
        output = document.getElementById("output");
        testWebSocket();
    }

    function testWebSocket() {
        websocket = new WebSocket(wsUri);
        websocket.onopen = function (evt) {
            onOpen(evt)
        };
        websocket.onclose = function (evt) {
            onClose(evt)
        };
        websocket.onmessage = function (evt) {
            onMessage(evt)
        };
        websocket.onerror = function (evt) {
            onError(evt)
        };
    }

    //连接
    function connect() {
        var messageId = getUID();
        var token = document.getElementById("token").value;
        var event = "connect";
        var message = {"source": 2, "data": token, "messageId": messageId, "event": event};
        console.log(message, token)
        //todo 这里需不需要toString
        writeToScreen(JSON.stringify(message));
        websocket.send(JSON.stringify(message));
    }

    //连接
    function disConnect() {
        var event = "dis_connect";
        var message = {"event": event};
        //todo 这里需不需要toString
        writeToScreen(JSON.stringify(message));
        websocket.send(JSON.stringify(message));
    }

    function sendMsg() {
        var messageId = getUID();
        var event = "sale_msg";
        var to = document.getElementById("to").value;
        var from = document.getElementById("from").value;
        var content = document.getElementById("content").value;
        var openid = document.getElementById("openId").value;
        var appCode = document.getElementById("appCode").value;
<<<<<<< HEAD
=======
        var userOpenId = document.getElementById("userOpenId").value;
        var fsOpenId = document.getElementById("fsOpenId").value;
        var appType = document.getElementById("appType").value;
        if (appType==1){
            //对应WeChatExtendDto 对象在chatsystem内
            var extend = {"openid":openid,"appCode":appCode};
            //对应ChatContentDto
            var data = {"msgType": "text", "content": content, "clientType": "pc","extend":extend};
            //对应messageDto
            var message = {"source": 2, "data": data, "messageId": messageId, "event": event, "to": to, "from": from};
            //todo 这里需不需要toString
            writeToScreen(JSON.stringify(message));
            websocket.send(JSON.stringify(message));
        }
        else {
            //对应DouyinExtendDto 对象在chatsystem内
            var extend = {"userOpenId":userOpenId,"fsOpenId":fsOpenId,"appType":4};
            //对应ChatContentDto
            var data = {"msgType": "text", "content": content, "userClientType": 4,"extend":extend};
            //对应messageDto
            var message = {"source": 2, "data": data, "messageId": messageId, "event": event, "to": to, "from": from};
            //todo 这里需不需要toString
            writeToScreen(JSON.stringify(message));
            websocket.send(JSON.stringify(message));
        }
>>>>>>> f514c0c... chatcontent修改


        //对应WeChatExtendDto 对象在chatsystem内
        var extend = {"openid":openid,"appCode":appCode};
        //对应ChatContentDto
        var data = {"msgType": "text", "content": content, "clientType": "pc","extend":extend};
        //对应messageDto
        var message = {"source": 2, "data": data, "messageId": messageId, "event": event, "to": to, "from": from};
        //todo 这里需不需要toString
        writeToScreen(JSON.stringify(message));
        websocket.send(JSON.stringify(message));
    }

    function onOpen(evt) {
        writeToScreen("CONNECTED");
    }

    function onClose(evt) {
        writeToScreen("DISCONNECTED");
    }

    function onMessage(evt) {

        let s = JSON.stringify(evt.data);
        writeToScreen('<span style="color: blue;">RESPONSE: ' + s + '</span>');
        console.log("收到消息：", JSON.parse(evt.data));

    }

    function onError(evt) {
        writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
        websocket.close();
        writeToScreen("DISCONNECTED");
    }

    function doSend(message) {
        writeToScreen("SENT: " + message);
        websocket.send(message);
    }

    function writeToScreen(message) {
        var pre = document.createElement("p");
        pre.style.wordWrap = "break-word";
        pre.innerHTML = message;
        output.appendChild(pre);
    }





    function getUID() { // 获取唯一值
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }

    window.addEventListener("load", init, false);
</script>
<h2>WebSocket Test</h2>
<div id="output"></div>

<input type="text" style="height: 150px;width: 300px;" id="content" name="content" placeholder="content">
<br>
<button onclick="sendMsg()">发送消息</button>
</html>